<script setup lang="ts">
import { openExternal } from "@/framework/util/shell";
import { hc } from "@/framework/vue/util/render";
import { CopyOutlined } from "@ant-design/icons-vue";
import ArchiveInstallOperation from "@/pages/tools/components/archive-install-operation.vue";
import { computed, onMounted, ref, shallowRef } from "vue";
import path from "path-browserify-esm";
import { getDragonwellJdkDownloadList } from "@/pages/tools/java/jdk-api";
import Grid from "@/components/framework/grid.vue";
import type { ToolInstallVersionItem } from "@/pages/tools/common/types";
import { useJdkStore } from "@/pages/tools/java/jdk-store";
import { copyText } from "@/pages/tools/common/util";

const jdkStore = useJdkStore();

const versionType = ref("standard");
const versionList = shallowRef<ToolInstallVersionItem[]>([]);
const renderVersionList = computed(() => versionList.value.filter(it => it.edition === versionType.value));

onMounted(() => {
  getDragonwellJdkDownloadList().then(list => (versionList.value = list));
});

function getSavePath(it: ToolInstallVersionItem) {
  return path.join(jdkStore.installPath, "dragonwell", it.name);
}

function isInstalled(it: ToolInstallVersionItem) {
  // TODO 判断jdk版本是否已安装
  return false;
}
</script>

<template>
  <grid rows-preset="two-rows-0">
    <a-form class="mb-16" layout="inline">
      <a-form-item label="版本类型">
        <div class="w-150">
          <a-select v-model:value="versionType">
            <a-select-option value="standard">标准版</a-select-option>
            <a-select-option value="extended">增强版</a-select-option>
          </a-select>
        </div>
      </a-form-item>
    </a-form>
    <div>
      <template v-for="it in renderVersionList" :key="it.name">
        <div class="tool-install-version-item">
          <a-space>
            <span class="hover-underline" @click.prevent="openExternal(it.url)">{{ it.name }}</span>
            <a-button :icon="hc(CopyOutlined)" size="small" type="text" title="复制下载地址" @click="copyText(it.url)" />
          </a-space>
          <archive-install-operation :id="it.id" :save-path="getSavePath(it)" :is-installed="isInstalled(it)" :download-url="it.url" />
        </div>
      </template>
    </div>
  </grid>
</template>
